/**
 * Created by Administrator on 2017/4/20.
 */


var Poker = require('../Poker/Poker');
var GD = require('../Poker/GD');
var PokerRoom = require('../Poker/room').PokerRoom;

var Const = require('./const');
var SubCode = require('../subCode');

var RoundStatus = {
    'WAITING': 1,
    'PREPARE': 2,
    'READY': 3,
};

exports.XQGDRoom = PokerRoom.extend({

    _roundStatus: RoundStatus.WAITING,
    _firstLeaveIndex: 0,        // 头游
    _lastLeaveIndex: 0,         // 末游
    _twoLeaveIndex: 0,          // 二游
    _threeLeaveIndex: 0,        // 三游

    init: function (rid, creator, msg) {
        this._super(rid, creator, msg);

        this._roundStatus = RoundStatus.WAITING;
        this._firstLeaveIndex = 0;
        this._lastLeaveIndex = 0;
        this._twoLeaveIndex = 0;
        this._threeLeaveIndex = 0;

        this.returnTribute = false;
        this.returnTributeTime = 0;
        this.tributeNum = 0;
    },

    /******************************************************************
     * 重载的接口
     ******************************************************************/

    /***
     * 获取本轮游戏信息
     * @returns {*}
     */
    getRoundInfo: function () {
        var info = this._super();
        info.selectWaitMS = this.getHideOption("selectWaitMS");
        return info;
    },

    /***
     * 获取房间配置
     * @returns {*}
     */
    getRoomConfig: function () {
        var cfg = this._super();
        cfg.respDestroyIgnore = 1;
        return cfg;
    },

    /***
     * 获取重连信息
     * @param rePlayer
     * @returns {*}
     */
    getReconnectInfo: function (rePlayer) {
        var info = this._super(rePlayer);
        info.firstLeaveIndex = this._firstLeaveIndex;
        info.lastLeaveIndex = this._lastLeaveIndex;
        return info;
    },

    getInfo: function () {
        var info = this._super();
        info.roomStatus = this._curStatus;
        return info;
    },

    /***
     * 开始新一局游戏之前
     */
    onBeforeStartNewRound: function () {
        this._super();
        this._firstLeaveIndex = 0;
        this._lastLeaveIndex = 0;
    },

    /***
     * 开始新一局游戏之后
     */
    onAfterStartNewRound: function () {
        this._super();
        if (!this._catCards) {
            this._preStart();
        }
    },

    _preStart: function () {
        this.initPlayersHandCards();

        if (this._firstLeaveIndex != 0 && this._lastLeaveIndex != 0) {
            this.onTribute();
        }
        this.startPlay();
    },

    startPlay: function () {
        this._curCardsInfo.playerIndex = this._diZhu;

        this.enumPlaying(function (eIndex, ePlayer) {
            ePlayer._jiaoStatus = Const.PlayerStatus.NONE;
        }.bind(this));
        this.broadcast("onStartPlayCard", {});
        this._changeCurPlay(this._diZhu);
    },

    /***
     * 处理进贡
     */
    onTribute: function () {
        var firstPlayer = this.getPlayerByIndex(this._firstLeaveIndex);
        var lastPlayer = this.getPlayerByIndex(this._lastLeaveIndex);

        var maxCard1 = lastPlayer.getTributeCard();
        if (this.tributeNum == 1) { // 只有一个人进贡
            if (lastPlayer.getHandCards().delCard(maxCard1)) {
                firstPlayer.addNewCard(maxCard1);
                this.broadcastReplay("onGetTribute", {
                    tributeNum: this.tributeNum,
                    firstPlayer: firstPlayer.getName(),
                    lastPlayer: lastPlayer.getName(),
                    maxCard1: maxCard1,
                });
            }
        } else if (this.tributeNum == 2) {
            var twoPlayer = this.getPlayerByIndex(this._twoLeaveIndex);
            var threePlayer = this.getPlayerByIndex(this._threeLeaveIndex);
            var maxCard2 = threePlayer.getTributeCard();
            if (lastPlayer.getHandCards().delCard(maxCard1) && threePlayer.getHandCards().delCard(maxCard2)) {
                var max = GD.compareMaxCard(maxCard1, maxCard2);
                var min = GD.compareMinCard(maxCard1, maxCard2);
                firstPlayer.addNewCard(max);
                twoPlayer.addNewCard(min);
                this.broadcastReplay("onGetTribute", {
                    tributeNum: this.tributeNum,
                    firstPlayer: firstPlayer.getName(),
                    lastPlayer: lastPlayer.getName(),
                    maxCard1: max,
                    maxCard2: min
                });
            }
        }

        this.broadcastReplay("onWaitReturnTribute", {
            firstPlayer: this._firstLeaveIndex,
            lastPlayer: this._lastLeaveIndex
        });
        this.returnTributeTime = setTimeout(function () {

        }.bind(this), this.getHideOption("selectTributeMS"));
    },

    /***
     * 处理进贡
     * @param player
     * @param msg
     * @param callback
     */
    handleReturnTribute: function (player, msg, callback) {
        if (!this.isPlaying()) {
            callback({code: Code.ROOM_NOT_PLAYING});
            return;
        }
        if (!player.isPlaying()) {
            callback({code: Code.PLAYER_NOT_PLAYING});
            return;
        }
        if (player.getIndex() != this._firstLeaveIndex) {
            callback({code: SubCode.NOT_CUR_PLAY});
            return;
        }
        if (!msg.hasOwnProperty('card')) {
            callback({code: Code.ARGS_INVALID});
            return;
        }

        callback({});

        var selectCard = msg.card;
        var lastPlayer = this.getPlayerByIndex(this._lastLeaveIndex);
        if (player.getHandCards().delCard(selectCard)) {
            lastPlayer.addNewCard(selectCard);
            broadcast("onReturnTribute", {
                firstPlayer: player.getIndex(),
                lastPlayer: this._lastLeaveIndex,
                card: selectCard
            });
        } else {
            callback({code: SubCode.CARD_NOT_EXIST});
            return;
        }

    },

    onPlayerJiaoDiZhu: function () {
        this.diZhuCards();
        var farmer1 = this.getPlayerByIndex(this.getNextIndex(this._diZhu));
        var farmer2 = this.getPlayerByIndex(this.getNextIndex(farmer1.getIndex()));
        farmer1._partner = farmer2.getIndex();
        farmer2._partner = farmer1.getIndex();
        logger.debug("room=%d onPlayerJiaoDiZhu dizhu = %d ,farmer1 = %d, farmer2 = %d", this.rid(), this._diZhu, farmer1.getIndex(), farmer2.getIndex());
    },

    diZhuCards: function () {
        var player = this.getPlayerByIndex(this._diZhu);
        var subConfig = this.getSubConfig();
        var remain = this._publicCards.getRemainNum();
        var sendCards = [];
        //发地主底牌
        for (var i = 0; i < remain; i++) {
            var card = this._publicCards.getCard();
            sendCards.push(card);
            player.addNewCard(card);
        }
        this._diZhuCards = sendCards;

        this.enumPlaying(function (eIndex, ePlayer) {
            var msg = {
                'cards': [],
                'playerIndex': this._diZhu,
                'num': sendCards.length
            };
            if (eIndex == this._diZhu) {
                msg.cards = sendCards;
            }
            ePlayer.send("onDiZhuCards", msg);
        }.bind(this));

    },
    handlePass: function (player, msg, callback) {
        if (!this.isPlaying()) {
            callback({code: Code.ROOM_NOT_PLAYING});
            return;
        }
        if (!player.isPlaying()) {
            callback({code: Code.PLAYER_NOT_PLAYING});
            return;
        }
        if (player.getIndex() != this._curPlay) {
            callback({code: SubCode.NOT_CUR_PLAY});
            return;
        }
        if (player._pass == false) {
            callback({code: SubCode.LXE_CANT_PASS});
            return;
        }
        callback({});
        var MSG = {
            'playerIndex': 0,
        };
        MSG.playerIndex = player.getIndex();
        this.broadcast("onPlayerPass", MSG);

        if (this._curStatus == Const.Status.JIAO) {
            player._tiStat = 0;
            var next = this.getNextPlaying(this._curPlay);
            if (next == this._dealerIndex) {
                this._doStartNewRound();
                return;
            }
            this._changeCurPlay();
        } else if (this._curStatus == Const.Status.TI) {
            player._tiStat = 0;
            player._tiCard = 0;
            var partner = this.getPlayerByIndex(player._partner);
            if (partner._tiStat < 0) {
                this._curStatus = Const.Status.TI;
                this._changeCurPlay(partner.getIndex());
            } else {
                this._curStatus = Const.Status.CHU;
                this.startPlay();
            }
        } else if (this._curStatus == Const.Status.GEN) {
            player._tiStat = 0;
            player._tiCard = 0;
            this._curStatus = Const.Status.HUI;
            this._changeCurPlay(this._diZhu);
        } else if (this._curStatus == Const.Status.HUI) {
            player._tiStat = 0;
            player._tiCard = 0;
            this._curStatus = Const.Status.CHU;
            this.startPlay();
        } else if (this._curStatus == Const.Status.CHU) {
            player._lastCards = {
                pass: 0
            };
            player._jiaoStatus = Const.PlayerStatus.BUCHU;
            this.broadcast("onPlayerStatusChange", {
                playerIndex: player.getIndex(),
                status: player._jiaoStatus
            });
            this._changeCurPlay();
        }

    },

    handleTiCard: function (player, msg, callback) {
        if (!this.isPlaying()) {
            callback({code: Code.ROOM_NOT_PLAYING});
            return;
        }
        if (!player.isPlaying()) {
            callback({code: Code.PLAYER_NOT_PLAYING});
            return;
        }
        if (player.getIndex() != this._curPlay) {
            callback({code: SubCode.NOT_CUR_PLAY});
            return;
        }
        if (this._curStatus == Const.Status.TI) {
            if (player.isTiSelected()) {
                callback({code: SubCode.LXE_PLAYER_IS_TI});
                return;
            }
        }
        if (!msg.hasOwnProperty('flag')) {
            callback({code: Code.ARGS_INVALID});
            return;
        }
        callback({});
        if (this._curStatus == Const.Status.TI) {
            if (msg.flag) {
                player._jiaoStatus = Const.PlayerStatus.TI;
                player._roundTi = Const.PlayerStatus.TI;
                player._tiCard = 1;
                player._tiStat = 1;
                player._curRate *= 2;
                /*this.broadcast("onPlayerTi",{
                 playerIndex: player.getIndex(),
                 flag: msg.flag
                 //curRate: this.getCurRate()
                 });*/
                this.broadcast("onPlayerStatusChange", {
                    playerIndex: player.getIndex(),
                    status: player._jiaoStatus
                });
                var partner = this.getPlayerByIndex(player._partner);
                if (partner._tiStat < 0) {
                    this._curStatus = Const.Status.GEN;
                    this._changeCurPlay(partner.getIndex());
                } else if (partner._tiStat == 0) {
                    this._curStatus = Const.Status.GEN;
                    this._changeCurPlay(partner.getIndex());
                } else {
                    this._curStatus = Const.Status.HUI;
                    this._changeCurPlay(this._diZhu);
                }
            } else {
                player._tiStat = 0;
                player._tiCard = 0;
                player._jiaoStatus = Const.PlayerStatus.BUTI;
                player._roundTi = Const.PlayerStatus.BUTI;

                /*this.broadcast("onPlayerTi",{
                 playerIndex: player.getIndex(),
                 flag: msg.flag
                 //curRate: this.getCurRate()
                 });*/
                this.broadcast("onPlayerStatusChange", {
                    playerIndex: player.getIndex(),
                    status: player._jiaoStatus
                });
                var partner = this.getPlayerByIndex(player._partner);
                if (partner._tiStat < 0) {
                    this._curStatus = Const.Status.TI;
                    this._changeCurPlay(partner.getIndex());
                } else {
                    this._curStatus = Const.Status.CHU;
                    this.startPlay();
                }
            }

        } else if (this._curStatus == Const.Status.GEN) {
            if (msg.flag) {
                player._tiCard = 1;
                player._tiStat = 1;
                player._jiaoStatus = Const.PlayerStatus.GEN;
                player._roundTi = Const.PlayerStatus.GEN;

                player._curRate *= 2;
                /*this.broadcast("onPlayerGen",{
                 playerIndex: player.getIndex(),
                 flag: msg.flag
                 });*/
                this.broadcast("onPlayerStatusChange", {
                    playerIndex: player.getIndex(),
                    status: player._jiaoStatus
                });

                this._curStatus = Const.Status.HUI;
                this._changeCurPlay(this._diZhu);
            } else {
                player._tiStat = 0;
                player._tiCard = 0;
                player._jiaoStatus = Const.PlayerStatus.BUGEN;
                player._roundTi = Const.PlayerStatus.BUGEN;

                this.broadcast("onPlayerStatusChange", {
                    playerIndex: player.getIndex(),
                    status: player._jiaoStatus
                });
                /*this.broadcast("onPlayerGen",{
                 playerIndex: player.getIndex(),
                 flag: msg.flag
                 });*/
                this._curStatus = Const.Status.HUI;
                this._changeCurPlay(this._diZhu);
            }

        } else if (this._curStatus == Const.Status.HUI) {
            if (msg.flag) {
                player._jiaoStatus = Const.PlayerStatus.HUI;
                player._roundTi = Const.PlayerStatus.HUI;

                player._tiCard = 1;
                player._tiStat = 1;
                this.enumPlayers(function (eIndex, ePlayer) {
                    if (eIndex == this._diZhu) {
                        return;
                    }
                    if (ePlayer.isTiCard()) {
                        ePlayer._curRate *= 2;
                    }
                });
                /*this.broadcast("onPlayerHui",{
                 playerIndex: player.getIndex(),
                 flag: msg.flag
                 //curRate: this.getCurRate()
                 });*/
                this.broadcast("onPlayerStatusChange", {
                    playerIndex: player.getIndex(),
                    status: player._jiaoStatus
                });

                this._curStatus = Const.Status.CHU;
                this.startPlay();
            } else {
                player._tiStat = 0;
                player._tiCard = 0;
                player._jiaoStatus = Const.PlayerStatus.BUHUI;
                player._roundTi = Const.PlayerStatus.BUHUI;

                /*this.broadcast("onPlayerHui",{
                 playerIndex: player.getIndex(),
                 flag: msg.flag
                 //curRate: this.getCurRate()
                 });*/
                this.broadcast("onPlayerStatusChange", {
                    playerIndex: player.getIndex(),
                    status: player._jiaoStatus
                });
                this._curStatus = Const.Status.CHU;
                this.startPlay();
            }
        }
        var dz = this.getPlayerByIndex(this._diZhu);
        dz._curRate = 0;
        this.enumPlaying(function (eIndex, ePlayer) {
            if (eIndex == this._diZhu) {
                return;
            }
            dz._curRate += ePlayer._curRate;
        }.bind(this));

        var players = {};
        this.enumPlaying(function (eIndex, ePlayer) {
            players[eIndex] = {};
            players[eIndex].curRate = ePlayer._curRate;
        }.bind(this));
        this.broadcast("onPlayerCurRateChange", {
            players: players
        });

    },
    onBeforePlayCard: function (player, msg, cards) {
        var pattern = msg.pattern;
        if (!pattern) {
            pattern = this.getPattern(cards);
            msg.pattern = pattern;
            if (!pattern) {
                return SubCode.PATTERN_ERROR;
            }
        }
        var code = 0;

        //检测玩家手中是否有这些牌
        var handCards = player._handCards.getCards();
        ExObject.eachKeyNum(cards, function (c, v) {
            if (!handCards.hasOwnProperty(c)) {
                code = SubCode.CARD_NOT_EXIST;
                return false;
            }
        });
        if (code) {
            return code;
        }
        //检测牌型与牌是否符合
        if (player.checkPattern(pattern, cards) == false) {
            return SubCode.PATTERN_ERROR;
        }
        //比牌
        if (player.getIndex() == this._curCardsInfo.playerIndex) {
            return 0;
        }
        if (pattern == this._curCardsInfo.pattern) {
            //var maxCard = player.getMaxCard(cards, pattern);
            if (player.getMaxCard(cards, pattern) > this._curCardsInfo.maxCard) {
                return 0;
            } else {
                return SubCode.LXE_CARDS_IS_SMALL;
            }
        } else if (pattern == Const.Pattern.SPEC_BOMB) {
            if (pattern > this._curCardsInfo.pattern) {
                return 0;
            } else {
                return SubCode.LXE_CARDS_IS_SMALL;
            }
        } else if (pattern == Const.Pattern.BOMB) {
            if (pattern > this._curCardsInfo.pattern) {
                return 0;
            } else {
                return SubCode.LXE_CARDS_IS_SMALL;
            }
        } else if (pattern == Const.Pattern.KING_BOMB) {
            if (pattern > this._curCardsInfo.pattern) {
                return 0;
            } else {
                return SubCode.LXE_CARDS_IS_SMALL;
            }
        } else {
            code = SubCode.PATTERN_ERROR;
        }
        return code;
    },
    _getObj: function (cards) {
        var obj = {};
        for (var i = 0; i < cards.length; i++) {
            ExObject.addNumber0(obj, cards[i].originValue, 1);
        }
        return obj;
    },
    _cardObjToArray: function (arrayObj) {
        var keys = Object.keys(arrayObj);
        var temp = [];
        keys.forEach(function (v) {
            if (arrayObj[v]) {
                temp.push(v);
            }
        });
        return temp;
    },
    handlePlayCard: function (player, msg, callback) {
        if (!this.isPlaying()) {
            callback({code: Code.ROOM_NOT_PLAYING});
            return;
        }
        if (!player.isPlaying()) {
            callback({code: Code.PLAYER_NOT_PLAYING});
            return;
        }
        if (player.getIndex() != this._curPlay) {
            callback({code: SubCode.NOT_CUR_PLAY});
            return;
        }
        if (this._curStatus != Const.Status.CHU) {
            callback({code: SubCode.LXE_CANT_PLAY_CARDS});
            return;
        }
        if (!msg.hasOwnProperty('cards')) {
            callback({code: Code.ARGS_INVALID});
            return;
        }
        logger.debug("room=%d onPlayCard %j,msg %j", this.rid(), this._curCardsInfo, msg);
        var cards = msg.cards;
        cards = this._getObj(cards);
        var code = this.onBeforePlayCard(player, msg, cards);
        if (code) {
            callback({code: code});
            return;
        }
        if (msg.pattern == Const.Pattern.TRIPLE) {
            if (player.getHandCardNum() != 3) {
                code = SubCode.PATTERN_ERROR;
                callback({code: code});
                return;
            }
        }
        callback({});
        this.saveInfo(msg, cards, player);
        var msgInfo = {
            cards: msg.cards,
            pattern: msg.pattern
        };
        this._curCardsInfo.lastPlayCards = msgInfo;
        player._pass = true;
        //将牌从玩家手牌中删除
        player.playCards(cards);
        //广播出牌消息
        var MSG = {
            'playerIndex': player.getIndex(),
            'cards': msg.cards,
            'pattern': msg.pattern
        };
        this.broadcast("onPlayCards", MSG);
        player._jiaoStatus = Const.PlayerStatus.CHU;
        this.broadcast("onPlayerStatusChange", {
            playerIndex: player.getIndex(),
            status: player._jiaoStatus
        });
        //出牌后处理，如果没赢，切换玩家
        if (player._handCards.isEmpty()) {
            if (player._isDiZhu) {
                this._diZhuWin = true;
            }
            if (this._diZhuWin) {
                var resp = true;
                this.enumPlayers(function (eIndex, ePlayer) {
                    if (eIndex == this._diZhu) {
                        return;
                    }
                    //console.log("len = " + ePlayer._stat._roundCards.length + "diz = " + this._diZhu);

                    if (ePlayer._stat._roundCards.length > 0) {

                        resp = false;
                        console.log("len = " + ePlayer._stat._roundCards.length + "diz = " + this._diZhu);

                    }

                }.bind(this));
                console.log("resp = " + resp);

                if (resp) {
                    this._spring = true;
                }
            } else {
                var diZhu = this.getPlayerByIndex(this._diZhu);
                console.log("len = " + diZhu._stat._roundCards.length);
                if (diZhu._stat._roundCards.length <= 1) {
                    this._spring = true;
                }
            }

            this.settlementCurRound(0);
        } else {
            this._changeCurPlay();
        }
    },
    getCardsNum: function (cards) {
        return ExObject.sumValue(cards);
    },

    saveInfo: function (msg, cards, player) {
        this._curCardsInfo.pattern = msg.pattern;
        this._curCardsInfo.cards = cards;
        this._curCardsInfo.playerIndex = player.getIndex();
        this._curCardsInfo.maxCard = player.getMaxCard(cards, msg.pattern);
        this._curCardsInfo.cardsNum = this.getCardsNum(cards);
        var tempCards = this._cardObjToArray(cards);

        player._lastCards = {
            pass: 1,
            cards: tempCards,
            pattern: msg.pattern
        };
        player._stat._roundCards.push(tempCards);
        if (msg.pattern == Const.Pattern.SPEC_BOMB || msg.pattern == Const.Pattern.BOMB || msg.pattern == Const.Pattern.KING_BOMB) {
            var maxRate = this.getOption("maxRate");
            player._bomb += 1;
            if (maxRate) {
                if (this._bombNum <= maxRate) {
                    this._bombNum += 1;
                    if (this._curCardsInfo.maxCard == 1 && this._curCardsInfo.cardsNum == 4) {
                        //this._bombNum += 2;
                        for (var i = 0; i < 3; i++) {
                            this.enumPlayers(function (eIndex, ePlayer) {
                                if (eIndex == this._diZhu) {
                                    return;
                                }
                                ePlayer._curRate *= 2;
                            });
                        }
                    } else {
                        this.enumPlayers(function (eIndex, ePlayer) {
                            if (eIndex == this._diZhu) {
                                return;
                            }
                            ePlayer._curRate *= 2;
                        });
                    }
                    var players = {};
                    this.enumPlaying(function (eIndex, ePlayer) {
                        players[eIndex] = {};
                        players[eIndex].curRate = ePlayer._curRate;
                    }.bind(this));
                    this.broadcast("onPlayerCurRateChange", {
                        players: players
                    });
                }
            } else {
                this._bombNum += 1;
                if (this._curCardsInfo.maxCard == 1 && this._curCardsInfo.cardsNum == 4) {
                    //this._bombNum += 2;
                    for (var i = 0; i < 3; i++) {
                        this.enumPlayers(function (eIndex, ePlayer) {
                            if (eIndex == this._diZhu) {
                                return;
                            }
                            ePlayer._curRate *= 2;
                        });
                    }
                } else {
                    this.enumPlayers(function (eIndex, ePlayer) {
                        if (eIndex == this._diZhu) {
                            return;
                        }
                        ePlayer._curRate *= 2;
                    });
                }
                var players = {};
                this.enumPlaying(function (eIndex, ePlayer) {
                    players[eIndex] = {};
                    players[eIndex].curRate = ePlayer._curRate;
                }.bind(this));
                this.broadcast("onPlayerCurRateChange", {
                    players: players
                });
            }
        }
    },

    onSettlement: function () {
        this._super();
        var room = this;
        /*this.enumPlayers(function(eIndex, ePlayer) {
         if(eIndex == room._diZhu){
         ePlayer.settlementPattern();
         }
         });*/
        var subConfig = room.getSubConfig();
        var di = subConfig.options.BASE;


        /*this.enumPlayers(function(eIndex, ePlayer) {
         if(ePlayer._tiCard == 1){
         pow += 1;
         }
         });*/
        var diZhu = this.getPlayerByIndex(this._diZhu);

        this.enumPlayers(function (eIndex, ePlayer) {
            if (eIndex == room._diZhu) {
                return;
            }
            var score = 0;
            score = ePlayer._curRate * di;

            if (room._spring) {
                console.log("spring = " + room._spring);
                score *= 2;
            }
            if (room._diZhuWin) {
                diZhu.addRoundScore(score);
                ePlayer.addRoundScore(-score);
            } else {
                diZhu.addRoundScore(-score);
                ePlayer.addRoundScore(score);
            }
        }.bind(this));

    },
    getPattern: function (cards) {
        var patterns = Const.Pattern;
        var player = this.getCurPlayer();
        var pattern = 0;
        ExObject.each(patterns, function (p, v) {
            if (player.checkPattern(v, cards)) {
                pattern = v;
            }
        });
        return pattern;
    },
    getSettlementInfo: function () {
        var info = this._super();
        var player = this.getPlayerByIndex(this.RoundWinPlayer);
        info.winIndex = this.RoundWinPlayer;
        info.diZhuCards = this._diZhuCards;
        return info;
    },

    onCurRoundFinished: function () {
        this._super();
        this._dealerIndex = this.getNextIndex(this._dealerIndex);
        this._curStatus = Const.Status.NONE;
        this.enumPlaying(function (eIndex, ePlayer) {
            ePlayer._lastCards = {
                pass: -1
            };
        }.bind(this));
        this._roundStatus = RoundStatus.WAITING;
    },

    /******************************************************************
     * 功能接口
     ******************************************************************/
    _changeCurPlay: function (playerIndex) {
        if (playerIndex) {
            this._curPlay = playerIndex;
        } else {
            this._curPlay = this.getNextIndex(this._curPlay);
        }
        var player = this.getPlayerByIndex(this._curPlay);
        if (player._jiaoStatus > 0) {
            player._jiaoStatus = Const.PlayerStatus.NONE;
        }
        if (this._curStatus == Const.Status.CHU) {
            console.log("card = %d cccccccccccccccccc cur = %d", this._curCardsInfo.playerIndex, this._curPlay);
            if (this._curPlay == this._curCardsInfo.playerIndex) {
                player._pass = false;
                this._roundTurns += 1;
                this.broadcast("onNewTurns", {
                    turns: this._roundTurns
                });
                this._curCardsInfo.lastPlayCards = {
                    cards: [],
                    pattern: 0
                };
                this.enumPlaying(function (eIndex, ePlayer) {
                    ePlayer._jiaoStatus = Const.PlayerStatus.NONE;
                    ePlayer._lastCards = {
                        pass: -1
                    };
                }.bind(this))
            }
        } else if (this._curStatus == Const.Status.JIAO) {
            if (player.haveBomb()) {
                player._pass = false;
            }
        }
        this._sendCurPlay(player._pass);
    },

    _startNewRoundAfter: function (timeMS) {
        if (this._roundStatus != RoundStatus.WAITING) {
            return;
        }

        this.broadcast("onRoomStartAfter", {
            time: timeMS,
        });

        this._roundStatus = RoundStatus.PREPARE;

        setTimeout(function () {
            if (this._roundStatus != RoundStatus.PREPARE) {
                return;
            }

            this._roundStatus = RoundStatus.READY;
            this.tryStartNewRound();
        }.bind(this), timeMS);
    },

    /**
     * 广播当前出牌玩家
     * @private
     */
    _sendCurPlay: function (pass) {
        var msg = {
            'curPlay': this._curPlay,
            'pass': true,
            'status': this._curStatus
        };
        if (!pass) {
            msg.pass = false;
        }
        this.broadcastReplay("onCurPlay", msg);
    },
});